package abq2.jic.jic.plugin.operate.AwardAndPunish;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class AwardAndPunish implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String , String> result = new HashMap<>();
        if ("GET_Award_Punish".equalsIgnoreCase(action)) {
            //获取学生的姓名和考试的名称
            String stu_name = params.get("stu_name");
            //获取DynamicObject列表，即获取学生的单据头的信息
            DynamicObject[] dys = BusinessDataServiceHelper.load("abq2_rew1",
                    "abq2_textfield3," +  //姓名
                            "abq2_textfield," +          //学号
                            "abq2_textfield1," +            //学生的班级
                            "abq2_combofield," +   //表现评价
                            "abq2_largetextfield," +     //获奖内容
                            "abq2_largetextfield1," +   //惩罚内容
                            "abq2_decimalfield111," +   //科目对应的成绩
                            "abq2_datefield," + //获奖日期
                            "abq2_datefield1,"  +  //惩罚日期
                             "abq2_combofield1",           //类型
                    (new QFilter("abq2_combofield1", QCP.is_notnull, null)).toArray());

            //创建一个JsonArray
            JSONArray jsonArray = new JSONArray();
            for (DynamicObject dynamicObject : dys) {
                //将学生的考试基本的信息加入JSONArray
                if ( stu_name.contains(dynamicObject.getString("abq2_textfield3")) ) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("stu_name", dynamicObject.getString("abq2_textfield3"));
                    jsonObject.put("stu_id", dynamicObject.getString("abq2_textfield"));
                    jsonObject.put("class_name", dynamicObject.getString("abq2_textfield1"));
                    if (dynamicObject.getDate("abq2_datefield") != null) {
                        jsonObject.put("award_time", new SimpleDateFormat("yyyy-MM-dd").format(dynamicObject.getDate("abq2_datefield")));
                    }else {
                        jsonObject.put("award_time",null);
                    }
                    if (dynamicObject.getDate("abq2_datefield1") != null) {
                        jsonObject.put("punish_time", new SimpleDateFormat("yyyy-MM-dd").format(dynamicObject.getDate("abq2_datefield1")));
                    }else {
                        jsonObject.put("punish_time",null);
                    }
                    jsonObject.put("type", dynamicObject.getString("abq2_combofield1"));
                    jsonObject.put("award_info",dynamicObject.getString("abq2_largetextfield"));
                    jsonObject.put("punish_info",dynamicObject.getString("abq2_largetextfield1"));
                    jsonArray.add(jsonObject);
                    //获取学生的成绩信息中的单据体信息，即获取学生的各门科目的成绩信息
                }
            }


            System.out.println(jsonArray.toJSONString());
            //加入resultDynamicObject参数，将JsonArray加入到这个参数当中，然后返回
            result.put("resultDynamicObject", jsonArray.toJSONString());
        }
        return result;
    }
}